package com.vstudy.zx.service.impl;

import com.vstudy.zx.dao.GradeDao;
import com.vstudy.zx.dao.SchoolDao;
import com.vstudy.zx.dao.SubjectDao;
import com.vstudy.zx.dao.TeacherDao;
import com.vstudy.zx.entity.Grade;
import com.vstudy.zx.entity.School;
import com.vstudy.zx.entity.Subject;
import com.vstudy.zx.entity.Teacher;
import com.vstudy.zx.service.TeacherService;
import com.vstudy.zx.utils.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private GradeDao gradeDao;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private SchoolDao schoolDao;

    @Override
    public int updatePwd(Teacher teacher) {
        return teacherDao.updatePwd(teacher);
    }

    @Override
    @Transactional(readOnly = true)
    public Teacher login(String username) {
        username = username.trim();
        return teacherDao.select(username);
    }

    @Override
    public Boolean updateImg(Teacher teacher, MultipartFile img, HttpServletRequest request) throws IOException {
        teacher = teacherDao.selectById(teacher.getTe_id());
        // 用户上传的图片后缀名
        String suffix = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
        if (teacher.getTe_image() != null) {  //如果存在，先删除
            String realpath = request.getServletContext().getRealPath(teacher.getTe_image());
            IOUtil.deleteFile(realpath);
        }
        String path = "/upload/teacher/img/";
        File dir = new File(request.getServletContext().getRealPath(path));
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String filename = UUID.randomUUID().toString();
        filename = filename + suffix;
        teacher.setTe_image(path + filename);
        String newRealPath = request.getServletContext().getRealPath(teacher.getTe_image());
        img.transferTo(new File(newRealPath));

        // 更新数据库
        int rs = teacherDao.updateImg(teacher);
        return rs > 0;
    }

    @Override
    public List<Grade> getGrades() {
        return gradeDao.select();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Subject> getSubjects() {
        return subjectDao.select();
    }

    @Override
    @Transactional(readOnly = true)
    public List<School> getSchools() {
        return schoolDao.selectAll();
    }

    @Override
    public Boolean exists(String loginName, Long excludeId) {
        Teacher teacher = teacherDao.selectByLoginName(loginName, excludeId);
        return teacher != null;
    }

    @Override
    public int update(Teacher teacher) {
        return teacherDao.update(teacher);
    }

    @Override
    public Boolean register(String realName, String username, String password) {
        int rs = teacherDao.add(realName, username, password);
        return rs > 0;
    }
}
